# _*_coding:utf-8_*_
#anyang-daima = 101180201
#http://www.weather.com.cn/data/sk/101180201.html    实时天气预报
#http://www.weather.com.cn/data/cityinfo/101180201.html   全天天气预报
import urllib.request, time
import json
from twilio.rest import Client

def get_info(url):
    weather = urllib.request.urlopen(url)
    data = weather.read().decode('utf-8')
    print(weather)
    ss = json.loads(data)
    return ss
def combine_info(actual,allday):
    city = '城市：%s'%actual['city']
    weather = '天气：%s天'%allday['weather']
    actual_temp = '实时温度：%s度'%actual['temp']
    wind = '风速:' + str(actual['WD']) + str(actual['WS'])
    shidu = '湿度：%s'%actual['SD']
    time = '时间：%s'%actual['time']
    max_temp = '最高气温:%s' %allday['temp2']
    min_temp = '最低气温：%s'%allday['temp1']
    combine_info = city + '\n'\
    + weather + '\n' + actual_temp + '\n' + min_temp + '\n' + max_temp + '\n' + wind + '\n'\
    + shidu + '\n' + time
    return combine_info
def Send_SMS(all_info):
    account_sid = "AC9e094a6ed2268facade109a556f9f752"
    auth_token = "c51ff7f0460b07c4b26019c252535634"
    client = Client(account_sid,auth_token)
    message =client.messages.create(
        to = "+8615737209526",
        from_= "+1323-612-0204",
        body = all_info
    )
    print(message.sid)

def judge_weather(actual,allday,all_info):
    weather = '晴,多云,阴'
    if allday['weather'] not in weather:
        all_info = '天气出现变化，请留意。\n' + all_info + '\nFrom--SKYNE'
        Send_SMS(all_info)
    if int(allday['temp1'][:-1]) < 5 or int(allday['temp2'][:-1]) > 30:
        all_info = '高温预警，请留意。\n' + all_info + '\nFrom--SKYNE'
        Send_SMS(all_info)

def main():
    actual_url = "http://www.weather.com.cn/data/sk/101180201.html"
    allday_url = "http://www.weather.com.cn/data/cityinfo/101180201.html"
    actual_info = get_info(actual_url)['weatherinfo']
    allday_info = get_info(allday_url)['weatherinfo']
    all_info = combine_info(actual_info,allday_info)
    judge_weather(actual=actual_info, allday=allday_info, all_info=all_info)
    print(actual_info,allday_info,all_info)


if __name__ == '__main__':
    main()





